﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using MarkdownDeep;

namespace MarkdownExplorer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Markdown _markdown;

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.Save();
            base.OnClosing(e);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string fileName = (string)Application.Current.Properties[App.FileNameKey];

            if (!String.IsNullOrEmpty(fileName))
            {
                LoadMarkdownFile(fileName);
            }
        }

        private void LoadMarkdownFile(string path)
        {
            if (File.Exists(path))
            {
                string input = File.ReadAllText(path);
                string output = Markdown.Transform(input);
                string documentXaml = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(output, true);
                //var flowDocument = (FlowDocument)Utils.ConvertXmlStringToMorphedObjectGraph(documentXaml);
                var flowDocument = (FlowDocument)System.Xaml.XamlServices.Parse(documentXaml);
                flowDocumentReader.Document = flowDocument;

                string documentText = Properties.Resources.Template.Replace("@Content", output);

                Title = String.Format(Properties.Resources.Application_TitleWithFileName, System.IO.Path.GetFileName(path));
            }
            else
                MessageBox.Show(this, String.Format(Properties.Resources.Application_FileNotFound, path), Properties.Resources.Dialog_Title, MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        private bool FileIsSupported(string path)
        {
            if (String.IsNullOrEmpty(path))
                return false;

            string extension = System.IO.Path.GetExtension(path).ToLowerInvariant();

            switch (extension)
            {
                case ".md":
                case ".mkd":
                    return true;
                default:
                    return false;
            }
        }

        private void OpenMenuItem_Click(object sender, ExecutedRoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = Properties.Resources.Dialog_FileFilter,
                FilterIndex = 2
            };

            if (openFileDialog.ShowDialog(this) == true)
            {
                LoadMarkdownFile(openFileDialog.FileName);
            }
        }

        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        #region Properties

        public Markdown Markdown
        {
            get
            {
                if (_markdown == null)
                {
                    var markDownUri = new Uri("/MarkdownExplorer;component/markdown.xaml", UriKind.Relative);

                    _markdown = (Markdown)Application.LoadComponent(markDownUri);
                }

                return _markdown;
            }
        }

        #endregion
    }
}